﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Server.TCP
{
    [AttributeUsage(AttributeTargets.Class)]
    public class CommandIDAttribute : Attribute
    {
        public int ID { get; set; }

        public CommandIDAttribute()
        {
        }

        public CommandIDAttribute(int id)
        {
            ID = id;
        }
    }

    public abstract class BusinessCommand
    {
        public abstract byte[] Excute(byte[] bytes);
    }

    public class CommandRegister
    {
        private static Dictionary<int, Type> _commandTypes;

        private static Dictionary<int, Type> CommandTypes
        {
            get
            {
                if (_commandTypes == null)
                    _commandTypes = new Dictionary<int, Type>();
                return _commandTypes;
            }
        }

        public void Register(Type type)
        {
            object[] attributes = type.GetCustomAttributes(typeof(CommandIDAttribute), true);
            if (attributes != null && attributes.Length > 0)
            {
                CommandIDAttribute attr = (CommandIDAttribute)attributes[0];
                CommandTypes[attr.ID] = type;
            }
        }

        public Type this[int id]
        {
            get
            {
                Type type;
                if (CommandTypes.TryGetValue(id, out type))
                    return type;
                return null;
            }
        }
    }
}
